package initialize

import (
	"gorm.io/driver/mysql"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"transfer-service/internal/conf"
	"transfer-service/internal/global"
	"transfer/pkg/data"
)

func InitMysql(c *conf.Bootstrap) {
	database := c.GetData().GetDatabase()
	var db *gorm.DB
	var err error
	switch database.Driver {
	case "postgres":
		db, err = gorm.Open(postgres.Open(database.Source), &gorm.Config{})
		if err != nil {
			global.LOG.Fatalf("failed opening connection to postgres: %v", err)
		}
	case "mysql":
		db, err = gorm.Open(mysql.Open(database.Source), &gorm.Config{})
		if err != nil {
			global.LOG.Fatalf("failed opening connection to mysql: %v", err)
		}
	}
	global.LOG.Info(database.Driver + "数据库连接成功")
	global.DB = db
	data.InitBaseMapper(db)
}
